package kr.pe.osdev.dao.jdbc.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import kr.pe.osdev.dao.SecondaryCodeDao;
import kr.pe.osdev.dao.jdbc.BaseJdbcDao;
import kr.pe.osdev.domain.SecondaryCode;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class SecondaryCodeDaoImpl extends BaseJdbcDao implements SecondaryCodeDao {

    @Override
    public SecondaryCode add(SecondaryCode secondaryCode) throws DataAccessException {
        String sql = getMessageSourceAccessor().getMessage("code.addPrimary");
        
        Object[] obj = new Object[] { secondaryCode.getPrimaryCode(),
                secondaryCode.getLocale(),
                secondaryCode.getPrimaryCode(),
                secondaryCode.getUseYn()};
  
        getJdbcTemplate().update(sql, obj);
        
        return secondaryCode;
    }

    @Override
    public int count() throws DataAccessException {
        return 0;
    }

    @Override
    public SecondaryCode find(SecondaryCode secondaryCode) throws DataAccessException {
        return null;
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<SecondaryCode> findList() throws DataAccessException {
        String sql = getMessageSourceAccessor().getMessage("code.secondary.findList");
        
        return (List<SecondaryCode>) getJdbcTemplate().query(sql, getRowMapper(false));
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public List<SecondaryCode> findCacheList() throws DataAccessException {
        String sql = getMessageSourceAccessor().getMessage("code.secondary.findCacheList");
        
        return (List<SecondaryCode>) getJdbcTemplate().query(sql, getRowMapper(true));
    }

    @Override
    public SecondaryCode modify(SecondaryCode secondaryCode) throws DataAccessException {
        return null;
    }

    @Override
    public SecondaryCode remove(SecondaryCode secondaryCode) throws DataAccessException {
        // TODO Auto-generated method stub
        return null;
    }

    private RowMapper getRowMapper(final boolean mode) {
        RowMapper rowMapper = new RowMapper() {
            public Object mapRow(ResultSet rs, int rownum) throws SQLException {
                return makeSecondaryCode(rs, mode);
            }
        };
        
        return rowMapper;
    }
    
    private SecondaryCode makeSecondaryCode(ResultSet rs, boolean mode) throws SQLException {
        SecondaryCode secondaryCode = new SecondaryCode();
        secondaryCode.setPrimaryCode(rs.getString("PRIMARY_CODE"));
        secondaryCode.setSecondaryCode(rs.getString("SECONDARY_CODE"));
        secondaryCode.setLocale(rs.getString("LOCALE"));
        secondaryCode.setCode(rs.getString("CODE"));
        secondaryCode.setCodeHieararchy(rs.getString("CODE_HIEARARCHY"));
        secondaryCode.setSecondaryCodeName(rs.getString("SECONDARY_CODE_NAME"));
        secondaryCode.setSecondaryCodeValue(rs.getString("SECONDARY_CODE_VALUE"));
        secondaryCode.setSecondaryCodeDescription(rs.getString("SECONDARY_CODE_DESCRIPTION"));
        secondaryCode.setUseYn(rs.getString("USE_YN"));
        
        if(mode) {
            secondaryCode.setIsLeaf(rs.getString("IS_LEAF"));
        }
        
        return secondaryCode;
    }    
}
